home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / dtype / fontdt12.lha / Src / prefs.c < prev    next >
C/C++ Source or Header  |  1996-05-06  |  5KB  |  171 lines

  1. /*
  2. **    prefs.c - prefrences handling for Font DataType class
  3. **    Copyright © 1995-96 Michael Letowski
  4. */
  5.  
  6. #include <exec/types.h>
  7. #include <exec/memory.h>
  8. #include <dos/dos.h>
  9. #include <dos/rdargs.h>
  10. #include <support/types.h>
  11. #include <support/dos.h>
  12.  
  13. #include <stdlib.h>
  14.  
  15. #include "classbase.h"
  16. #include "prefs.h"
  17.  
  18. #include <proto/exec.h>
  19. #include <proto/dos.h>
  20.  
  21.  
  22. /*
  23. **    Private constants and macros
  24. */
  25. #define BUF_SIZE            1024                                            /* Size of prefs file buffer */
  26.  
  27. #define MAX_SUB_CNT        3                                                    /* Max number of sub-options */
  28.  
  29. #define PREFS_PATH1        "PROGDIR:Prefs/DataTypes/font.prefs"
  30. #define PREFS_PATH2        "ENV:DataTypes/font.prefs"
  31.  
  32.  
  33. /*
  34. **    Private functions prototypes
  35. */
  36. STATIC LBOOL ReadPrefs(struct ClassBase *cb, struct PrefsHandle *ph,
  37.                                                 struct Opts *opts, STRPTR name);
  38. STATIC LBOOL ReadSubPrefs(struct ClassBase *cb, struct RDArgs *rda,
  39.                                                     STRPTR argStr, STRPTR template,
  40.                                                     LONG fields[], LONG optCnt);
  41. STATIC struct RDArgs *ReadArgsBuf(struct ClassBase *cb, STRPTR template,
  42.                                                                     LONG *opts, struct RDArgs *rda);
  43.  
  44.  
  45. /*
  46. **    Public functions
  47. */
  48. struct PrefsHandle *GetFontPrefs(struct ClassBase *cb, struct Opts *opts)
  49. {
  50.     struct PrefsHandle *PH;
  51.     LBOOL Res=FALSE;
  52.  
  53.     clear(opts);                                                                    /* Set defaults */
  54.  
  55.     try(new(PH,MEMF_CLEAR),                                                        NO_PREFS);
  56.     try(PH->ph_RDA1=AllocDosObject(DOS_RDARGS,NULL),    NO_RDA1);
  57.     try(PH->ph_RDA2=AllocDosObject(DOS_RDARGS,NULL),    NO_RDA2);
  58.  
  59.     if(ThisProcessS->pr_HomeDir)                                    /* PROGDIR: exists? */
  60.         Res=ReadPrefs(cb,PH,opts,PREFS_PATH1);            /* Try first path */
  61.     unless(Res)                                                                        /* Args not read so far */
  62.         ReadPrefs(cb,PH,opts,PREFS_PATH2);                    /* Try second path */
  63.     return(PH);
  64.  
  65.     catch(FOO,            );
  66.     catch(NO_RDA2,    FreeDosObject(DOS_RDARGS,PH->ph_RDA2));
  67.     catch(NO_RDA1,    FreeDosObject(DOS_RDARGS,PH->ph_RDA1));
  68.     catch(NO_PREFS,    delete(PH));
  69.     return(NULL);
  70. }    /* GetFontPrefs */
  71.  
  72. VOID FreeFontPrefs(struct ClassBase *cb, struct PrefsHandle *ph)
  73. {
  74.     FreeArgs(ph->ph_Args);                                                /* Free args (may be NULL) */
  75.     FreeDosObject(DOS_RDARGS,ph->ph_RDA2);                /* Free 2nd RDA structure */
  76.     FreeDosObject(DOS_RDARGS,ph->ph_RDA1);                /* Free 1st RDA structure */
  77.     delete(ph);
  78. }    /* FreeFontPrefs */
  79.  
  80.  
  81. /*
  82. **    Private functions
  83. */
  84. STATIC LBOOL ReadPrefs(struct ClassBase *cb, struct PrefsHandle *ph,
  85.                                                 struct Opts *opts, STRPTR name)
  86. {
  87.  
  88.     struct RDArgs *RDA1=ph->ph_RDA1;
  89.     struct RDArgs *RDA2=ph->ph_RDA2;
  90.     STRPTR Buf,Buffer;
  91.     LONG Len,Size;
  92.     BPTR FH;
  93.  
  94.     try(FH=Open(name,MODE_OLDFILE),                            NO_FILE);
  95.     try(Buffer=AllocMem(BUF_SIZE,MEMF_PUBCLR),    NO_BUF);
  96.  
  97.     /* Consolidate multiple lines into a single buffer */
  98.     Buf=Buffer;
  99.     Size=BUF_SIZE-1;
  100.     while(Size>0 && FGets(FH,Buf,Size))
  101.     {
  102.         Len=strlen(Buf);
  103.         if(Buf[Len-1]=='\n')    Buf[Len-1]=' ';
  104.         Buf+=Len;
  105.         Size-=Len;
  106.     }
  107.  
  108.     /* Set up all RDA structures */
  109.     RDA1->RDA_Source.CS_Buffer=Buffer;                        /* Set up buffer to read from */
  110.     RDA2->RDA_Source.CS_Buffer=Buffer;                        /* Set up buffer to read from */
  111.  
  112.     /* Read args */
  113.     if(ph->ph_Args=ReadArgsBuf(cb,TEMPLATE,(LONG *)opts,RDA1))
  114.     {
  115.         opts->opt_DPIFlag=
  116.             ReadSubPrefs(cb,RDA2,opts->opt_DPIStr,TEMPLATEDPI,&opts->opt_XDPI,2);
  117.         opts->opt_ForeFlag=
  118.             ReadSubPrefs(cb,RDA2,opts->opt_ForeStr,TEMPLATECOL,opts->opt_ForeCol,3);
  119.         opts->opt_BackFlag=
  120.             ReadSubPrefs(cb,RDA2,opts->opt_BackStr,TEMPLATECOL,opts->opt_BackCol,3);
  121.     }
  122.  
  123.     catch(ERROR,        );
  124.     catch(NO_BUF,        FreeMem(Buffer,BUF_SIZE));
  125.     catch(NO_FILE,    Close(FH));
  126.     return(FH ? TRUE : FALSE);                                        /* True if prefs file exists */
  127. }    /* ReadPrefs */
  128.  
  129. STATIC LBOOL ReadSubPrefs(struct ClassBase *cb, struct RDArgs *rda,
  130.                                                     STRPTR argStr, STRPTR template,
  131.                                                     LONG fields[], LONG optCnt)
  132. {
  133.     LONG *SubOpts[MAX_SUB_CNT];
  134.     struct RDArgs *Args;
  135.     LONG I;
  136.     LBOOL Res=FALSE;
  137.  
  138.     if(argStr)
  139.     {
  140.         strcpy(rda->RDA_Source.CS_Buffer,argStr);
  141.         if(Args=ReadArgsBuf(cb,template,(LONG *)SubOpts,rda))
  142.         {
  143.             Res=TRUE;
  144.             for(I=0; I<optCnt; I++)
  145.                 if(SubOpts[I])
  146.                     fields[I]=*SubOpts[I];
  147.             FreeArgs(Args);
  148.         }
  149.     }
  150.     return(Res);
  151. }    /* ReadSubPrefs */
  152.  
  153. STATIC struct RDArgs *ReadArgsBuf(struct ClassBase *cb, STRPTR template,
  154.                                                                     LONG *opts, struct RDArgs *rda)
  155. {
  156.     STRPTR Buffer=rda->RDA_Source.CS_Buffer;
  157.     LONG Len;
  158.  
  159.     /* Prepare buffer */
  160.     Len=strlen(Buffer);                                                        /* Get size of buffer */
  161.     Buffer[Len]='\n';                                                            /* New line char */
  162.     Buffer[Len+1]='\0';                                                        /* String terminator */
  163.  
  164.     /* Prepare RDArgs */
  165.     rda->RDA_Source.CS_Length=Len+1;                            /* Set up string len */
  166.     rda->RDA_Source.CS_CurChr=0;                                    /* Set up current char */
  167.     rda->RDA_Buffer=NULL;                                                    /* As documented in AutoDocs */
  168.     fset(rda->RDA_Flags,RDAF_NOPROMPT);                        /* Disable prompts */
  169.     return(ReadArgs(template,opts,rda));                    /* Read arguments */
  170. }    /* ReadArgsBuf */
  171.